====== 2014 ======

{{indexmenu>.#1}}

====== April: Multiscale simulators ======

I gave two Doebeli-Hauert lab meeting talks about my investigation into stochastic simulation packages for solving agent-based multiscale elementary reaction kinetics.

I'm looking for a stochastic simulation engine.  Here are the features I'd like:
  * easy to use;
  * spatial (and maybe network) support;
  * allow nonlocal interactions because space could represent phenotype, eg. $A(x) + B(y) \rightarrow C(z)$;
  * fast/efficient; and
  * handles stiff problems accurately.

<sortable>
^ Package                                                                                                                                                        ^ Last Reviewed  ^ Research                                                                                                                            ^ Platform and Requirements       ^ Structure?                                                                                     ^ Pros                        ^ Cons                                                                                                                                                               ^ Additional Notes                                                                                   ^
| [[http://engineering.ucsb.edu/~cse/StochKit/| StochKit2]]                                                                                                      | 2014-04-11     | [[http://bioinformatics.oxfordjournals.org.ezproxy.library.ubc.ca/content/27/17/2457| Sanft et al., 2011]]                          | Windows or Linux/Unix/Mac OS X  | Well mixed only                                                                                | Supports "slow-scale SSA".  |                                                                                                                                                                    |                                                                                                    |
| [[http://www.smoldyn.org/| Smoldyn]]                                                                                                                           | 2014-03-14     | [[http://www.ploscompbiol.org/article/info%3Adoi%2F10.1371%2Fjournal.pcbi.1000705| Andrews et al., 2010]]                           | Custom language.                | Spatial                                                                                        | Easy to install/use.        | Fixed timestep; not adaptive.  Only reaction-diffusion; no nonlocal interactions.  Finite number of species; species can't have continuously-variable properties.  | Lotka-Volterra demos:\\ * examples\S6_commands\cmdtime1.txt\\ * examples\S8_reactions\lotvolt\...  |
| [[http://nba.uth.tmc.edu/cds/index.htm| Cellular Dynamic Simulator (CDS)]]                                                                                     | 2014-04-04     | [[http://link.springer.com.ezproxy.library.ubc.ca/article/10.1007%2Fs12021-010-9066-x| Byrne et al., 2010]]                         | Java                            | Spatial                                                                                        |                             | Inactive since 2010.                                                                                                                                               |                                                                                                    |
| [[http://steps.sourceforge.net/STEPS/default.php|  STEPS (STochastic Engine for Pathway Simulation)]]                                                          | 2014-03-28     | [[http://www.biomedcentral.com.ezproxy.library.ubc.ca/1752-0509/6/36/abstract| Hepburn et al., 2012]]                               | Python                          | Spatial                                                                                        |                             | Needs separate 3D modeler to implement spatial structure "meshes".                                                                                                 | Unofficial [[http://www.lfd.uci.edu/~gohlke/pythonlibs/#steps| Windows binaries]] available.       |
| [[http://sourceforge.net/apps/trac/urdme/| URDME]]                                                                                                             | 2014-02-28     | [[http://www.biomedcentral.com.ezproxy.library.ubc.ca/1752-0509/6/76| Drawert et al., 2012]]                                        | MATLAB, GCC                     | Spatial                                                                                        |                             | No Windows support.                                                                                                                                                |                                                                                                    |
| [[http://www.stochss.org/| StochSS]]                                                                                                                           | 2014-02-21     | [[http://www.sciencedirect.com/science/article/pii/S0021999111001094| Hellander and Lötstedt, 2011]]                                | Python, Amazon EC2              | Well mixed only                                                                                |                             | Windows version doesn't run locally -- only on Amazon cloud.\\ Suggests it uses URDME to do spatial sims but not yet supported.                                    |                                                                                                    |
| [[http://gfrd.org/| eGFRD (enhanced Green’s Function Reaction Dynamics)]]                                                                                      | 2014-01-31     | [[http://scitation.aip.org.ezproxy.library.ubc.ca/content/aip/journal/jcp/123/23/10.1063/1.2137716| van Zone and ten Wolde, 2005]]  | C++ and (or?) Python            | Continuous space                                                                               |                             | Project seems inactive.\\ Visualization tools appear to be for Linux only.                                                                                         |                                                                                                    |
| [[http://ccl.northwestern.edu/netlogo/| NetLogo]] with [[http://modelingcommons.org/browse/one_model/3850#model_tabs_browse_discuss| per-capita tau-leaping]]  | 2013-10-08     | none                                                                                                                                | NetLogo                         | Continuous or discrete space, network.\\ Reactions can be nonlocal: eg. A(x) + B(y) --> C(z).  | Easy to install/use.        | Slow.  "per-capita tau leaping" algorithm untested for validity or accuracy.                                                                                       |                                                                                                    |

</sortable>

Here are some example problems I'd like to be able to model:
  * Michaelis-Menton (well-mixed or local interactions),
  * Spatial [[http://www.labandtheory.com/kinetics/kinetics-reaction-mechanisms-and-the-iodine-clock-reaction/ | iodine clock]] (local interactions),
  * [[http://rspb.royalsocietypublishing.org/content/274/1608/347 | Doebeli et al., 2007]] (spatial, non-local), or
  * any case where a subset of reactions occur much faster than others.

These kinds of problems run very slowly in Gillespie's SSA because the fast processes swamp the dynamics.  Fixed timestep techniques (eg. Smoldyn) don't work well because they can be stiff, for example when one of the species is depleted in the iodine clock.  Standard tau-leaping and next reaction methods (I think) suffer the same problem as SSA for stiff problems.  R-leaping ([[http://scitation.aip.org.ezproxy.library.ubc.ca/content/aip/journal/jcp/125/8/10.1063/1.2218339 | Auger et al., 2006]]) is probably the same.  

Slow scale tau-leaping ([[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC2753989/ | Cao and Petzold, 2008]]) looks promising but can it handle non-local interactions?  I also need to have another look at [[http://scitation.aip.org.ezproxy.library.ubc.ca/content/aip/journal/jcp/118/11/10.1063/1.1545446 | Rao and Arkin, 2003]].

===== Why multiscale and nonlocal? =====

Multiscale and nonlocal reactions each present challenges on their own and I'm sure each is being tackled on its own.  Why do I insist on a tool that can handle both?  Isn't that just asking too much?  Maybe.  But it looks like "real" problems will require both.  

<WRAP box round>
==== An example ====

Consider [[http://rspb.royalsocietypublishing.org/content/274/1608/347 | Doebeli et al., 2007]].  We describe a population $\phi(x)$ of individuals, with continuous trait $x$, that obeys
\[
\begin{array}{rcll}
  \frac{d}{dt} \phi(x) & = & B(x) - D(x) & \text{ where } \\
  B(x) & = & \int du \frac{\phi(u)}{N(u)} \int dv \phi(v) A(u-v) G_{\frac{u+v}{2},\sigma}(x) & \text{ is the birthrate, } \\
  D(x) & = & \frac{1}{K(x)} \int dy \alpha(x-y) \phi(x) \phi(y) & \text{ is the death rate, } \\
  N(u) & = & \int dv A(u-v) \phi(v) & \text{ is the effective density of mating partners, }
\end{array}
\]
$A(\cdot)$ is the assortment kernel, and $G(\cdot)$ is the Gaussian distribution.

It's not obvious what the behaviours of the individuals are that lead to this dynamic.  I prefer to limit my thinking to [[wp>elementary reaction| elementary reactions]] so that interactions occur according to [[wp>collision theory]].  This seems like a good first principle since it is easy to interpret/justify biologically and mathematically convenient.

=== Death process ===

The death rate is straightforward to construct an elementary reaction from.  Let $\Phi$ represent a single individual in the population.  Death can be written as
\[
\begin{array}{rcll}
  \Phi(x) + \Phi(y) \xrightarrow{\frac{1}{K(x)} \alpha(x-y)} \Phi(y) & \text{ (death process). }
\end{array}
\]
Notice this reaction is nonlocal: the deathrate of $\Phi(x)$ depends on all other individuals $\Phi(y)$ for all phenotypes $y$.

=== Birth process ===

However, the existence of the factor $N(\cdot)$ involving $\phi$ in the denominator of the birthrate means that it's not possible to do the same for a birth process --- elementary reactions can only produce polynomial expressions.  It will be necessary to invoke another type of individual in our model.

We need to introduce another type but there is no systematic way to do so.  We have a "many to one" problem: there are many types and interactions we could introduce that would lead to the same population dynamic under some conditions.  We choose gamete production: Let $E$ be an "egg" involved in the following elementary reactions:
\[
\begin{array}{rcll}
  \Phi(u) & \xrightarrow{1}   & \Phi(u) + E(u)     & \text{(egg production)} \\
  \Phi(v) + E(u) & \xrightarrow{A(u-v) G_{\frac{u+v}{2},\sigma}(x)}   & \Phi(v) + \Phi(x)     & \text{(birth).}
\end{array}
\]
As with death, the birth process is spatial (in phenotype space) and nonlocal.

The dynamics of this system is then
\[
\begin{array}{rcl}
  \frac{d}{dt} e(u) & = & \phi(u) - e(u) N(u) \\
  \frac{d}{dt} \phi(x) & = & \int du e(u) \int dv \phi(v) A(u-v) G_{\frac{u+v}{2},\sigma}(x) - D(x).
\end{array}
\]

Here we have reconstructed a set of elementary reactions that reproduces the dynamics in [[http://rspb.royalsocietypublishing.org/content/274/1608/347 | Doebeli et al., 2007]] in the appropriate limit (as will be shown below).  But it does more that.  It also:
  - Gives an explanation for the terms we see in [[http://rspb.royalsocietypublishing.org/content/274/1608/347 | Doebeli et al., 2007]] from first principles, and
  - Shows how that model can be extended naturally into a more general case.
</WRAP>

I hope the above convinces you that nonlocal methods, at least, are required to handle interesting problems.  But it may not convince that the methods need be multiscale.  
You might argue that mathematical techniques like timescale separation (eg. quasi-steady-state approximation) reduce the complexity of the problem.

For example, the problem of [[http://rspb.royalsocietypublishing.org/content/274/1608/347 | Doebeli et al., 2007]] can be reduced in the case where assortment is strong ($A(\cdot)$ large) because $e(u)$ equilibrates very quickly to
\[
  e(u) \approx \phi(u) / N(u)
\]
so the species $E$ can be removed from the reactions and the birth processes reduce to
\[
\begin{array}{rcll}
  \Phi(u) + \Phi(v) & \xrightarrow{A(u-v) G_{\frac{u+v}{2},\sigma}(x) / N(u)}   & \Phi(u) + \Phi(v) + \Phi(x)     & \text{birth under fast assortment}
\end{array}
\]

The above may seem to introduce a fast process itself, since it involves $A(\cdot)$ in the reaction rate, but in fact it is not.  Remember that $N(u)$ in the denominator is also proportional to $A(\cdot)$ so the birth process remains "slow" no matter how fast assortment becomes.

With this birth and the original death process we have reactions that only occur on the slow timescale so stiffness is no longer a barrier to efficient simulation.  Of course, one needs to keep track of $N(u)$ which adds some overhead but it can be done.

So what's the problem?  While it's great to use what we know (eg. that there exists a timescale separation) to reduce a complicated model, I have a few objections to relying on this method:

  - Timescale separation methods (eg. QSSA) are not well founded ([[http://epubs.siam.org.ezproxy.library.ubc.ca/doi/abs/10.1137/1031091 | Segel and Slemrod, 1989]]; [[http://link.springer.com.ezproxy.library.ubc.ca/article/10.1140/epjst/e2013-01882-3 | Das and Ray, 2013]]).  They seem to work in cases even when they're not justified.  That can be a good thing but it is unsettling not to have a firm foundation.
  - The simplest timescale separation methods drop stochasticity in the fast species, simply replacing them with their equilibrium values.  But it should be possible to add appropriate fluctuations around the equilibrium so this isn't a big issue, I think.
  - These methods only give correct solutions in limiting cases.  General results (eg. when there isn't complete separation of timescales or the degree of separation depends on local concentrations in a heterogeneous environment) can't be handled.
  - You have to know in advance that you're dealing with a multiscale problem and reduce the model accordingly.  You can't just use the simulation tools to explore possible models.

Whether (and how much) you are sympathetic to the above objections will be a matter of taste.  For me, the last is my biggest objection.  I'd like to have a general simulator I can naively throw models into to and explore experimentally for interesting behaviour **before** I try to understand why I'm seeing that behaviour.

===== Feedback =====

I wasn't effective in getting my message across.  Nevertheless, some good ideas came my way:
  * Christoph suggested looking into Langevin methods.  I had a look but [[http://link.springer.com.ezproxy.library.ubc.ca/chapter/10.1007/978-3-540-68894-5_5 | Gillespie, 2008]] Figure 2 suggests that it might be moving too far to large populations.  It might fail to describe small populations, such as you might see during the fast transition in the iodine clock.  On the other hand, [[http://arxiv.org/abs/1406.2502 | Schnoerr et al., 2014]] seems to have a firm foundation for extending the Langevin equation to small(er) populations.
  * Stilian had a very interesting idea.  In gravitation and plasma physics simulations they use a [[http://obswww.unige.ch/lastro/conferences/sf2013/pdf/lecture2.pdf | tree algorithm]] to bundle influences from far-ranging sources.  This might be a great way to incorporate nonlocality.  I don't know how it would handle multiscale problems.


====== May: Ecological games ======

Reza's talk last month got me thinking about how to mix ecology and game theory.  I pointed him to some work ([[random_research:rik_s_notes:replicator_kinetics:start#select_births_with_a_separate_game]]) I had done on the topic.  I then used some basic ideas from that for my "Cooperation" lecture on the retreat (see %%R:\NetLogo\2014-05 isci320 v*.nlogo%%).  I've got four assumptions in that work that I might want to relax, to see what happens:

  - Games are played much more often than ecological processes (births/deaths) occur, 
  - The last game is remembered forever, 
  - Agents don't spontaneously die, and
  - Logistic dynamics emerge from competitive deaths.

I could relax each with the following changes:

  - Reduce the game playing rate $\gamma$,
  - Allow agents to forget their state, and default to a zero birthrate, eg. $Z_{ij} \rightarrow Z_{i0}$,
  - Add a spontaneous (ecological, ie. not state-dependent) deathrate, eg. $Z_{ij} \rightarrow \emptyset$, and
  - Introduce competition for birth niches by only allowing births when an empty niche (eg. $E$) is available, eg. $Z_{ij} + E \rightarrow 2 Z_{ij}$.

I don't know how important any of these choices would be.  It might be good to explore each at least in the mean field case to get a handle on the consequences.


====== May: A Precise formulation of logistic kinetics ======

Reza mentioned a $K+1/2$ anomaly in his recent presentation.  When he simulated logistic dynamics he found the population fluctuated around a density of $K + 1/2$, instead of the expected carrying capacity, $K$.  I've seen similar issues in my simulations where the sample equilibrium is noticeably different (usually below) the mathematical expectation.  Why?  One problem Reza had was that he had individuals competing with themselves (ie. a deathrate proportional to $n^2$ instead of $n(n-1)$).  But that doesn't explain $K+1/2$.  As we'll see, it should have given a $K\pm 1$ not $K+1/2$.

So let's go through the derivation, carefully. Consider the reactions
\begin{eqnarray}
  N   & \xrightarrow{\beta}  & 2 N & \text{(birth)} \\
  2 N & \xrightarrow{\alpha} & N   & \text{(competition).} 
\end{eqnarray}


===== Mean field =====

The mean field approximation (in the limit of a large, well-mixed system) for the density $n$ of individuals is
\begin{eqnarray}
  \frac{dn}{dt} & = & \beta n - \alpha n (n-1) \\
                & = & (\beta + \alpha) n - \alpha n^2 \\
                & = & r n \left( 1 - \frac{n}{K}\right),
\end{eqnarray}
where
\begin{eqnarray}
  r & = & \beta + \alpha           & \text{ is the intrinsic growth rate and} \\
  K & = & \frac{\beta}{\alpha} + 1 & \text{ is the carrying capacity.} \label{eq:carryingcap}
\end{eqnarray}

Note: If we had allowed competition with oneself so that $\frac{dn}{dt} = \beta n - \alpha n^2$, then we would have arrived at $K=\beta/\alpha$, instead.


===== Detailed balance =====

We have a model involving transitions between integer states (population counts).  Transitions only occur between neighbouring states.  The lowest non-trivial state is $n=1$.  

^              Birth rate ^  Population  ^ Death rate                  ^
|                         |  $n$         |                             |
|  $(n-1) \beta \uparrow$ |              | $\downarrow n(n-1) \alpha$  |
|                         |  $n-1$       |                             |
|                         |  $\vdots$    |                             |
|      $2 \beta \uparrow$ |              | $\downarrow 6 \alpha$       |
|                         |  $2$         |                             |
|        $\beta \uparrow$ |              | $\downarrow 2 \alpha$       |
|                         |  $1$         |                             |
|                         |  $0$         |                             |
Notice that the state $n=0$ is isolated -- there are no transitions to or from this state because births can only occur where an individual already exists and death only occurs due to competition, which requires the presence of at least two individuals.

We're interested in the steady-state.  For $n=1$ to be in a steady-state the flow rate of the probability in must equal the flow rate out, or $\beta P(1) = 2 \alpha P(2)$.  Likewise, in steady-state all flow rates between two neighbouring states must balance:
\begin{equation}
  \beta P(n-1) = n \alpha P(n).
\end{equation}
This criterion is called [[wp>detailed balance]].  Because of the particulars of this system, it is required for an equilibrium distribution of probabilities, $P(n)$.

Since detailed balance holds in equilibrium we have a recursion relation that uniquely defines the distribution:
\begin{equation}
  P(n) = (K-1) \frac{P(n-1)}{n} = (K-1)^{n-1} \frac{P(1)}{n!}
\end{equation}
where we have borrowed $\beta/\alpha = K-1$ from Equation \ref{eq:carryingcap} and 
\begin{equation}
  1 = \sum_{n=0}^\infty P(n) = P(0) + \frac{e^{K-1} - 1}{K-1} P(1).
\end{equation}
Thus,
\begin{eqnarray}
  P(1) & = & (1 - P(0)) \frac{K-1}{e^{K-1} - 1} \\
  P(n) & = & (1 - P(0)) \frac{(K-1)^n}{ \left( e^{K-1} - 1 \right) n!}.
\end{eqnarray}

The expected density in equilibrium is then
\begin{equation}
  \bar{n} = \sum_{n=0}^\infty n P(n) = (1-P(0)) (K-1) \frac{e^{K-1}}{e^{K-1} - 1}.
\end{equation}
When $K\gg 1$ then $\bar{n}\approx (1-P(0)) (K-1)$ but $\bar{n}(K=1) = 1-P(0)$.  (Since we've defined $K=\beta/\alpha + 1$ it is not meaningful to take $K<1$.)
